<?
/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

require_once("classes_general.inc");

class Database
{
	var $dblLink;
	var $dbresResouce;

// 	var $m_objMysqli;
// 	var $m_objResults;

	function Database()
	{
		$this->dblLink = mysql_connect(DB_HOST, DB_USER, DB_PASSWD);
		if (!$this->dblLink)
		{
			die(mysql_error());
		}
		mysql_select_db(DB_DATABASE, $this->dblLink);
		$this->dbresResource = 0;
	}
// 	public function __construct()
// 	{
// 		$this->objMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_DATABASE);
// 		$this->m_objResults = null;
// 	}

// 	public function __destruct()
// 	{
// 		if (null != $this->m_objResults)
// 		{
// 			$this->m_objResults->free();
// 		}
// 	}

	function Destruct()
	{
		mysql_close($this->dblLink);
// 		if (null != $this->m_objResults)
// 		{
// 			$this->m_objResults->free();
// 		}
	}

	function ExecuteSQL($sSQL)
	{
		if (false != $this->dbresResource)
		{
			//mysql_free_result($this->dbresResource);
		}
		$this->dbresResource = mysql_query($sSQL);
		if (false != $this->dbresResource)
		{
			if (true == ereg("^select", $sSQL))
			{
				$nRows = mysql_num_rows($this->dbresResource);
			}
			else
			{
				$nRows = true;
			}
		}
		else
		{
			$nRows = false;
		}
		return $nRows;

// 		if (null != $this->m_objResults)
// 		{
// 			$this->m_objResults->free();
// 		}
// 		$this->objResults = $this->objMysqli->query($strSQL);
// 		if (false == $this->objResults)
// 		{
// 			$nError = $this->objMysqli->errno;
// 			$strError = $this->objMysqli->error;
// 			// throw new Exception("[Database] : $nError : $strError");
// 		}
// 		//
// 		// if an insert, update, etc. statement is successful,
// 		// mysqli->query returns true but objResults isn't an object;
// 		// therefore, we have to see if $this->objResults->num_rows exists
// 		//
// 		if (true == is_object($this->objResults))
// 		{
// 			$nRows = $this->objResults->num_rows;
// 		}
// 		else
// 		{
// 			$nRows = 0;
// 		}
// 		return $nRows;
// 		// return $nRows;
	}

	function NextRow()
	{
		$aRow = mysql_fetch_assoc($this->dbresResource);
		return $aRow;
// 		$aRow = $this->objResults->fetch_array();
// 		return $aRow;
	}

// 	function GetFieldCount()
// 	{
// 		$nFields = $this->objResults->field_count;
// 		return $nFields;
// 	}

// 	function GetFields()
// 	{

// 		$aFields = array();
// 		$nFields = $this->GetFieldCount();
// 		for ($i = 0;
// 			 $i < $nFields;
// 			 $i++)
// 		{
// 			$oField = $this->objResults->fetch_field_direct($i);
// 			$aFields[$i] = $oField->name;
// 			$aFields[$oField->name] = $i;
// 		}
// 		return $aFields;
// 	}

	function EscapeString($sContent)
	{
		return mysql_real_escape_string($sContent, $this->dblLink);
		// return $this->objMysqli->real_escape_string($strContent);
	}

	function ExecuteSQLGetResults($sSQL)
	{
		$nRows = $this->ExecuteSQL($sSQL);
		if (false == $nRows)
		{
			return array();
		}
		else
		{
			$aResults = array();
			for ($i = 0;
				 $i < $nRows;
				 $i++)
			{
				$aRow = $this->NextRow();
				$aResults[$i] = $aRow;
			}
			return $aResults;
		}
	}

	function Error()
	{
		return mysql_error();
	}
}

?>